<?php
/**
 * 获取
 *
 * 
 */
 
use Workerman\Worker;
use Workerman\Mysql;

class get_poster_contents{
	
	public function entry($param){
		
		return $this->do_get_poster_contents($param);
	}

	// 如果是总监，返回结果数据和处理逻辑如下：
	// 结果数据：[{"name":"干货1部","sub":[{"name":"第1页","sub":[{"name":"广佛版","id":"","picture_path":"","contents":""},{"name":"粤西版","id":"","picture_path":"","contents":""}]},{"name":"第2页","sub":[{"name":"粤东版","id":"","picture_path":"","contents":""},{"name":"钦州版","id":"","picture_path":"","contents":""}]}]},{"name":"干货2部","sub":[{"name":"第1页","sub":[{"name":"广佛版","id":"","picture_path":"","contents":""},{"name":"粤西版","id":"","picture_path":"","contents":""}]},{"name":"第2页","sub":[{"name":"粤东版","id":"","picture_path":"","contents":""},{"name":"钦州版","id":"","picture_path":"","contents":""}]}]}]

	// 处理逻辑：1、找到总监下所有分部门
	//          2、通过_poster_contents_id 和分部门，找到所有的分部门、页数、区域、picture_department_id、picture_path、contents
	//          2、把数据按照分部门->页数->区域、picture_department_id、picture_path、contents排列，并且把数据返回给前端。


	/** 
	* do_get_poster_contents  
	* 查询当前用户所在部门，需要审核当前次要校的所有图片
	* [{"name":"第1页","sub":[{"name":"广佛版","id":"","picture_path":"","contents":""},{"name":"粤西版","id":"","picture_path":"","contents":""}]},{"name":"第2页","sub":[{"name":"粤东版","id":"","picture_path":"","contents":""},{"name":"钦州版","id":"","picture_path":"","contents":""}]}]
	* @access private 
	* @param $param 
	* @since 1.0 
	* @return array(errcode,errmsg,rtv) 
	*/
	private function do_get_poster_contents($param){

        $id = $param["id"];
		$user_id = $param["user_id"];
		// 是否是总监
		$is_chief_inspector = 0;
		$history = @$param["history"];

		if($history == 1){
			$sql = "SELECT d.id,c.picture_path,c.page,c.area,d.contents,b.poster_id FROM hf_mes_poster_contents b 
INNER JOIN hf_mes_poster_picture c ON b.id=c.poster_contents_id 
INNER JOIN hf_mes_poster_picture_department d ON c.id =d.picture_id 
INNER JOIN  hf_mes_user e ON d.department_id=e.department 
WHERE e.id='$user_id' AND b.id='$id'  AND d.finish=1";

            $user_sql = "SELECT c.department_headquarters_id,b.class FROM hf_mes_user a 
					inner join hf_mes_groups b ON a.groups=b.id 
					inner join hf_mes_department c ON a.department=c.id
					where a.id='$user_id'";
            $dp = sql_query($user_sql);
            // group为1、2、3,分别代表采购员、副总监、总监
            //$department_headquarters_id = $dp[0]["department_headquarters_id"];
            $department_headquarters_id = $dp[0]["department_headquarters_id"];
            $group = $dp[0]["class"];
            if($group == "总监"){
                $sql_str = "d.finish=1";
                //$sql_str = "d.chief_inspector='-1'";
                $is_chief_inspector = 1;
            }

		}else{

			$sql = "SELECT c.department_headquarters_id,b.class FROM hf_mes_user a 
					inner join hf_mes_groups b ON a.groups=b.id 
					inner join hf_mes_department c ON a.department=c.id
					where a.id='$user_id'";
			$dp = sql_query($sql);
			// group为1、2、3,分别代表采购员、副总监、总监
			$department_headquarters_id = $dp[0]["department_headquarters_id"];
			$group = $dp[0]["class"];
			$sql_str = "d.finish=0";
			if($group == "副总监"){
				$sql_str = "d.finish=1 AND d.deputy_director='-1'";
			}elseif($group == "总监"){
                $sql_str = "d.chief_inspector='-1' AND d.deputy_director!='-1'";

				//$sql_str = "d.chief_inspector='-1'";
				$is_chief_inspector = 1;
			}

			$sql = "SELECT d.id,c.picture_path,c.page,c.area,d.contents,b.poster_id FROM hf_mes_poster_contents b 
					INNER JOIN hf_mes_poster_picture c ON b.id=c.poster_contents_id 
					INNER JOIN hf_mes_poster_picture_department d ON c.id =d.picture_id 
					INNER JOIN  hf_mes_user e ON d.department_id=e.department 
					WHERE e.id='$user_id' AND b.id='$id'  AND $sql_str";

		}
		
		if($is_chief_inspector == 1){
			// 找到总监下的所有分部门信息
			$sql = "SELECT e.name,d.id,c.picture_path,c.page,c.area,d.contents,b.poster_id FROM hf_mes_poster_contents b 
			INNER JOIN hf_mes_poster_picture c ON b.id=c.poster_contents_id 
			INNER JOIN hf_mes_poster_picture_department d ON c.id =d.picture_id
			INNER JOIN hf_mes_department e ON d.department_id =e.id
			WHERE b.id='$id'  AND d.department_id IN (SELECT id FROM hf_mes_department where department_headquarters_id='$department_headquarters_id') AND $sql_str";
			$ret=sql_query($sql);
			// 结果数据：[{"name":"干货1部","sub":[{"name":"第1页","sub":[{"name":"广佛版","id":"","picture_path":"","contents":""},{"name":"粤西版","id":"","picture_path":"","contents":""}]},{"name":"第2页","sub":[{"name":"粤东版","id":"","picture_path":"","contents":""},{"name":"钦州版","id":"","picture_path":"","contents":""}]}]},{"name":"干货2部","sub":[{"name":"第1页","sub":[{"name":"广佛版","id":"","picture_path":"","contents":""},{"name":"粤西版","id":"","picture_path":"","contents":""}]},{"name":"第2页","sub":[{"name":"粤东版","id":"","picture_path":"","contents":""},{"name":"钦州版","id":"","picture_path":"","contents":""}]}]}]

			$ret_values = [];
			$obj = new stdClass();
			foreach($ret as $key=>$val){
				if(!property_exists($obj,$val["name"])){
					$obj->{$val["name"]} = new stdClass(); 
					$obj->{$val["name"]}->{$val["page"]} = [];
				}else if(!property_exists($obj->{$val["name"]},$val["page"])){
					$obj->{$val["name"]}->{$val["page"]} = [];
				}
				array_push($obj->{$val["name"]}->{$val["page"]},[$val["area"],$val["id"],$val["picture_path"],$val["contents"]]);
			}
			// $obj结构
			// {"干货1部":{"第1页":[["广佛版","id","图片路径","内容"]],"第2页":[["广佛版","id","图片路径","内容"]]},"干货2部":{"第1页":[["广佛版","id","图片路径","内容"]],"第2页":[["广佛版","id","图片路径","内容"]]}}
			$ret_values = [];
			// 部门
			foreach($obj as $keys=>$values){
				$obj_department = new stdClass();
				$obj_department->name = $keys;
				$obj_department->sub = [];
				// 页数
				foreach($values as $key => $val){
					$obj_page = new stdClass();
					$obj_page->name = $this->exchange_page($key);
					$obj_page->sub = [];
					// 区域
					foreach($val as $v){
						$obj_area = new stdClass();
						$obj_area->name = $this->exchange_area($v[0]);
						$obj_area->id = $v[1];
						$obj_area->picture_path = $v[2];
						$obj_area->contents = $v[3];
						array_push($obj_page->sub,$obj_area);
					}
					array_push($obj_department->sub,$obj_page);
				}
				array_push($ret_values,$obj_department);
			}
			return array("errcode"=>4005, "errmsg"=>"", "rtval"=>$ret_values);
		}else{
			$dp=sql_query($sql);
			//var_dump($dp);
			$ret = new stdClass();
			foreach($dp as $val){
				if(!property_exists($ret,$val["page"])){
					$ret->{$val["page"]} = [];
					
				}
				array_push($ret->{$val["page"]},[$val["area"],$val["id"],$val["picture_path"],$val["contents"],$val["poster_id"]]);
			}

			$ret_values = [];
			foreach($ret as $key=>$val){
				$obj = new stdClass();
				$obj->name = $this->exchange_page($key);
				$obj->sub = [];
				foreach($val as $v){
				    var_dump($v);
					$obj_sub = new stdClass();
					$obj_sub->name = $this->exchange_area($v[0]);
					$obj_sub->id = $v[1];
					$obj_sub->picture_path = $v[2];
					$obj_sub->contents = $v[3];
					$obj_sub->poster_management_id = $v[4];
					array_push($obj->sub,$obj_sub);
				}
				array_push($ret_values,$obj);
			}
		}
		
        return array("errcode"=>0, "errmsg"=>"", "rtval"=>$ret_values);
		
	}


	// 转换页数
	private function exchange_page($page){
		return "第".$page."页";
	}

	// 转换区域
	private function exchange_area($area){
		$text = "";
		if($area == "1"){
			$text = "广佛版";
		}else if($area == "2"){
			$text = "粤东版";
		}else if($area == "3"){
			$text = "粤西版";
		}else if($area == "4"){
			$text = "钦州版";
		}
		return $text;
	} 
	 
	 
	/** 
	* create  
	* 通过 call_user_func 获得本class的实例
	* 
	* @access public 
	* @since 1.0 
	* @return object 本类的实例
	*/	
    public static function create(){  
        static $instance;
        if (!$instance){  
			$instance = new get_poster_contents();  
        }
		return $instance;  
    }   
 }
 

?>